package com.demo.user.controller;

import com.demo.user.dao.LoginUserDaoJpa;
import com.demo.user.dto.UserDetailDto;
import com.demo.user.dto.UserDto;
import com.demo.user.dto.UserPageDto;
import com.demo.user.dto.UserPageParamDto;
import com.demo.user.entity.User;
import com.demo.user.response.Response;
import com.demo.user.response.ResponseFactory;
import com.demo.user.service.JpaUserService;
import com.demo.user.service.UserService;
import com.demo.user.util.page.BasePaginationRsp;
import com.demo.user.validation.annotation.OnValid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Pageable;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 用户(BlUser)表控制层
 *
 * @author makejava
 * @since 2023-03-24 14:32:24
 */
// @Controller 将当前修饰的类注入SpringBoot IOC容器，使得从该类所在的项目跑起来的过程中，这个类就被实例化。
// @ResponseBody 它的作用简短的说就是指该类中所有的API接口返回的数据，甭管你对应的方法返回Map或是其他Object，它会以Json字符串的形式返回给客户端
//@RestController 是@controller和@ResponseBody 的结合
@RestController
@RequestMapping("/user")
@Api(
    tags = {"UserController"},
    description = "用户curd简单测试")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private JpaUserService jpaUserService;
    @Autowired
    private UserService userService;


    /**
     * 列表查询-JPA模式
     *
     * @param userDto 筛选条件
     */
    @GetMapping("getListJpa")
    public ResponseEntity<List<LoginUserDaoJpa>> getListJpa(UserDto userDto) {
        return ResponseEntity.ok(jpaUserService.getUserListJpa(userDto));
    }

    /**
     * 分页查询-JPA模式
     *
     * @param userDto 筛选条件
     */
    @GetMapping("getListPageJpa")
    public Response<Object> getListPageJpa(UserDto userDto, @PageableDefault(size = 6, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable) {
        return ResponseFactory.toSuccess(jpaUserService.getUserListByPageJpa(userDto,pageable));
    }

    @GetMapping("queryUserById/{id}")
    @ApiOperation(value = "按id查询用户", notes = "按id查询数据", httpMethod = "GET")
    public User queryUserById(@PathVariable(value="id")Long id) {
        return userService.queryUserById(id);
    }

    @GetMapping("queryAllUser")
    @ApiOperation(value = "查询所有用户", notes = "查询所有用户数据", httpMethod = "GET")
    public List<Map<String, Object>> queryAllUser() {
        return userService.queryAllUser();
    }


    @GetMapping("detail/{id}")
    @ApiOperation(value = "按id查询用户详情", notes = "按id查询数据，格式化返回参数", httpMethod = "GET")
    public Response<UserDetailDto> detail(@PathVariable(value="id")Long id) {
        return  ResponseFactory.toSuccess(userService.detail(id));
//        return userService.detail(id);
    }


    @PostMapping("/page")
    @ApiOperation("用户分页列表")
    public Response<BasePaginationRsp<UserPageDto>> page(
            @OnValid @RequestBody UserPageParamDto pageParam
    )
    {
        return ResponseFactory.toSuccess(userService.page(pageParam));
    }


}

